﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ocmf.Types
{
    public class ImagePlaceholder:Panel
    {
        private Point placeholderMoveOrigin;
        
        public ImagePlaceholder()
        {
            BorderStyle = BorderStyle.FixedSingle;
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            var gfx = e.Graphics;
            gfx.SmoothingMode = SmoothingMode.AntiAlias;

            var font = new Font("Tahoma", 15);
            var stringSize = SizeF.Empty;

            do
            {
                font = new Font("Tahoma", font.Size + 1);
                stringSize = gfx.MeasureString(Parent.Controls.IndexOf(this).ToString(), font);
            } while (stringSize.Width < Width && stringSize.Height < Height);

            gfx.TextRenderingHint = TextRenderingHint.AntiAlias;
            gfx.DrawString(Parent.Controls.IndexOf(this).ToString(), font, Brushes.Black, (Width - stringSize.Width) / 2, (Height - stringSize.Height) / 2);

        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            var delta = new Point(((placeholderMoveOrigin.X - e.X) / ((ScreenTemplate)Parent).GridSize) * ((ScreenTemplate)Parent).GridSize, ((placeholderMoveOrigin.Y - e.Y) / ((ScreenTemplate)Parent).GridSize) * ((ScreenTemplate)Parent).GridSize);

            if (e.Button == MouseButtons.Left)
            {
                if (Cursor == Cursors.PanNorth)
                {
                    Top -= delta.Y;
                    Height += delta.Y;
                }
                else if (Cursor == Cursors.PanEast)
                    Width = (e.X / ((ScreenTemplate)Parent).GridSize) * ((ScreenTemplate)Parent).GridSize;
                else if (Cursor == Cursors.PanSouth)
                    Height = (e.Y / ((ScreenTemplate)Parent).GridSize) * ((ScreenTemplate)Parent).GridSize;
                else if (Cursor == Cursors.PanWest)
                {
                    Left -= delta.X;
                    Width += delta.X;
                }
                else if (Cursor == Cursors.SizeAll)
                {
                    Top -= delta.Y;
                    Left -= delta.X;
                }

                Refresh();
            }
            else
            {
                placeholderMoveOrigin = e.Location;

                if (e.X > Width - 5 && e.X < Width) //cursor is on the right side
                    Cursor = Cursors.PanEast;
                else if (e.Y > 0 && e.Y < 5) //cursor is on top side
                    Cursor = Cursors.PanNorth;
                else if (e.X > 0 && e.X < 5) //cursor is on the left side
                    Cursor = Cursors.PanWest;
                else if (e.Y > Height - 5 && e.Y < Height) //cursor is on the bottom side
                    Cursor = Cursors.PanSouth;
                else //cursor resides within panel
                    Cursor = Cursors.SizeAll;
            }
        }
    }
}
